package io.dshubs.archives.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.dshubs.archives.dao.ArchiveBaseInfoMapper;
import io.dshubs.archives.dto.ArchiveBaseCategoryDto;
import io.dshubs.archives.entity.ArchiveBaseCategory;
import io.dshubs.archives.dao.ArchiveBaseClassMapper;
import io.dshubs.archives.entity.ArchiveBaseInfo;
import io.dshubs.archives.service.ArchiveBaseCategoryService;
import io.dshubs.commons.mybatis.service.impl.BaseServiceImpl;
import io.dshubs.commons.security.context.TenantContext;
import io.dshubs.commons.security.user.SecurityUser;
import io.dshubs.commons.security.user.UserDetail;
import io.dshubs.commons.tools.enums.SuperAdminEnum;
import io.dshubs.commons.tools.enums.SuperTenantEnum;
import io.dshubs.commons.tools.exception.RenException;
import io.dshubs.commons.tools.utils.ConvertUtils;
import io.dshubs.commons.tools.utils.TreeUtils;
import io.dshubs.dto.SysDictDataDTO;
import io.dshubs.feign.DictFeignClient;
import io.dshubs.feign.UserFeignClient;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 全宗类信息表 服务实现类
 * </p>
 *
 * @author tan
 * @since 2021-08-16
 */
@Service
public class ArchiveBaseCategoryServiceImpl extends BaseServiceImpl<ArchiveBaseClassMapper, ArchiveBaseCategory> implements ArchiveBaseCategoryService {

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Autowired
    private ArchiveBaseInfoMapper archiveBaseInfoMapper;

    @Override
    public List<ArchiveBaseCategoryDto> list(Map<String, Object> queryMap) {


        List<ArchiveBaseCategory> archiveBaseClassList = this.baseDao.selectList(getWrapper(queryMap,true));
        if(CollectionUtils.isEmpty(archiveBaseClassList)) {
            return new ArrayList<>();
        }
        List<ArchiveBaseCategoryDto> dtos = ConvertUtils.sourceToTarget(archiveBaseClassList, ArchiveBaseCategoryDto.class);
        dtos.forEach(item->{
            SysDictDataDTO data = dictFeignClient.get(item.getDeadLine()).getData();
            item.setDeadLineStr( data == null?null:data.getDictLabel());

            SysDictDataDTO secretData = dictFeignClient.get(item.getSecret()).getData();
            item.setSecretStr( secretData == null?null:secretData.getDictLabel());

            SysDictDataDTO typeData = dictFeignClient.get(item.getType()).getData();
            item.setTypeStr( typeData == null?null:typeData.getDictLabel());
        });
        return TreeUtils.build(dtos);
    }

    private Wrapper<ArchiveBaseCategory> getWrapper(Map<String, Object> queryMap,Boolean flag){
        UserDetail user = SecurityUser.getUser();
        List<Long> ids = null;

        String archiveId = (String) queryMap.get("archiveId");
        String code = (String) queryMap.get("code");
        String name = (String) queryMap.get("name");
        QueryWrapper<ArchiveBaseCategory> queryWrapper = new QueryWrapper<ArchiveBaseCategory>().eq("tenant_code", TenantContext.getTenantCode(user));
        if(StringUtils.isEmpty(archiveId) && flag){
            List<ArchiveBaseInfo> list = archiveBaseInfoMapper.selectList(
                    new QueryWrapper<ArchiveBaseInfo>().eq("tenant_code", user.getTenantCode()));
            if(CollectionUtils.isNotEmpty(list)) {
                archiveId = String.valueOf(list.get(0).getId());
            }
        }
        if(user.getSuperTenant() != SuperTenantEnum.YES.value()) {
            ids = userFeignClient.getCategoryId().getData();
        }
        if(StringUtils.isNotEmpty(archiveId) ) {
            queryWrapper.eq("archive_id",archiveId);
        }
        if(StringUtils.isNotEmpty(code)){
            queryWrapper.eq("code",code);
        }
        if(StringUtils.isNotEmpty(name)){
            queryWrapper.like("name",name);
        }
        if(CollectionUtils.isNotEmpty(ids)){
            queryWrapper.in("id",ids);
        }
        return queryWrapper;
    }

    @Override
    public List<Long> getArchiveBaseCategoryId() {
        UserDetail user = SecurityUser.getUser();
        if(user.getSuperTenant() != SuperTenantEnum.YES.value()) {
            return userFeignClient.getCategoryId().getData();
        }
        return null;
    }

    @Override
    public List<ArchiveBaseCategory> listNoTree(Map<String, Object> params) {
        return this.baseDao.selectList(getWrapper(params,false));
    }

    @Override
    public List<Map<String, String>> getClassField(Long id) {
        if(id == null) {
            throw new RenException("参数异常");
        }
        ArchiveBaseCategory archiveBaseCategory = this.selectById(id);
        if(archiveBaseCategory == null) {
            throw new RenException("数据异常");
        }
        return null;
    }

}
